package com.cosmoplat.xikai.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cosmoplat.xikai.business.domain.BaseDeliveryInfo;
import com.cosmoplat.xikai.business.domain.BaseSafetyStock;
import com.cosmoplat.xikai.business.domain.dto.materialRequirement.MaterialRequirementQueryDTO;
import com.cosmoplat.xikai.business.domain.dto.materialRequirement.MaterialStockModifyDTO;
import com.cosmoplat.xikai.business.domain.vo.MaterialRequirementVo;
import com.cosmoplat.xikai.business.enums.MaterialEnum;
import com.cosmoplat.xikai.business.service.BaseDeliveryInfoService;
import com.cosmoplat.xikai.business.service.BaseSafetyStockService;
import com.cosmoplat.xikai.business.service.MaterialRequirementService;
import com.cosmoplat.xikai.common.enums.DelFlagEnum;
import com.cosmoplat.xikai.common.exception.ServiceException;
import com.cosmoplat.xikai.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName MaterialRequirementServiceImpl
 * @Description 物料需求 服务实现类
 * @Author wdf
 * @Date 2025/1/7
 */

@Slf4j
@Service
public class MaterialRequirementServiceImpl  implements MaterialRequirementService {

    @Autowired
    private BaseDeliveryInfoService deliveryInfoService;

    @Autowired
    private BaseSafetyStockService safetyStockService;

    @Override
    public List<MaterialRequirementVo> queryForPage(MaterialRequirementQueryDTO queryDTO) {
        List<MaterialRequirementVo> list = deliveryInfoService.queryMaterialRequirement(queryDTO);

        List<BaseSafetyStock> safetyStockList = safetyStockService.getSafetyStockList();
        if (CollectionUtils.isEmpty(safetyStockList)) {
            return list;
        }
        Map<String, BigDecimal> safetyStockMap = safetyStockList.stream().collect(Collectors.toMap(BaseSafetyStock::getMaterialCode, BaseSafetyStock::getBaseSafetyStockQty));
        for (MaterialRequirementVo materialRequirementVo : list) {
            if (safetyStockMap.containsKey(materialRequirementVo.getMaterialCode())) {
                materialRequirementVo.setSafeStockQty(safetyStockMap.get(materialRequirementVo.getMaterialCode()));
            }
        }
        return list;
    }

    @Override
    public void modifyMaterialStock(MaterialStockModifyDTO stockModifyDTO) {
        BaseDeliveryInfo deliveryInfo = deliveryInfoService.getById(stockModifyDTO.getId());
        if (deliveryInfo == null) {
            throw new ServiceException("不存在对应的配送物料");
        }
        if(!MaterialEnum.inStockMaterial(deliveryInfo.getMaterialCode())) {
            throw new ServiceException("该物料不能修改库存");
        }
        log.info("修改配送物料ID为{}的库存为{}", deliveryInfo.getMaterialCode(), stockModifyDTO.getStockQty());
        deliveryInfo.setStockQty(stockModifyDTO.getStockQty());
        deliveryInfoService.updateById(deliveryInfo);
    }
}
